const app = getApp()
Page({
  data: {
    loading: true,
    active:0,
    page: 0,
    cate_id: 0,
    icons: [],
    stores: [],
  },
  //监听页面初次载入完成
  onLoad: function (event) {
    this.getApiCate(event.cate_id);
  },
  //下拉刷新
  onPullDownRefresh: function () {
    wx.showNavigationBarLoading();
    setTimeout(() => {
      wx.hideNavigationBarLoading();
      wx.stopPullDownRefresh();
    }, 1500);
    this.setData({page: 0,loading: true,stores: []});
    this.getApiStroe();
  },
  //底部加载更多
  onReachBottom: function () {
    this.setData({
      loading: true,
    });
    this.getApiStroe();
  },
  //点击Tab切换
  onChange: function (event) {
    this.setData({
      loading: true,
      page: 0,
      cate_id: event.detail.name,
      stores: [],
    })
    this.getApiStroe();
  },
  //行业分类
  getApiCate: function (parent_id) {
    var param = {
      signkey: app.util().getRandom(12),
      cate_id: parent_id,
      lists:1
    }
    app.api().Get('api/v4/allwin/store/cate', param,(result) => {
      if (result.code == 200) {
        this.setData({
          icons: result.data,
          cate_id: result.data[0].id,
        })
        this.getApiStroe();
      }
    })
  },
  /**
   * 获取店铺
   */
  getApiStroe: function () {
    if (this.data.loading) {
      var param = {
        page: this.data.page + 1,
        cate_id: 0,
        cate_sid: this.data.cate_id,
      }
      app.api().Get('api/v4/allwin/store/lists', param,(result) =>{
        if (result.code == 200) {
          var stores = this.data.stores;
          for (let i in result.data) {
            stores.push(result.data[i]);
          }
          this.setData({
            stores: stores,
            page:param.page,
          });
        }
        this.setData({
          loading: false
        })
      })
    }
  },
  //搜索
  onSearch: function (event) {
    if (app.util().isNull(event.detail)) {
      app.wxLayer('请输入好店关键词')
    } else {
      wx.navigateTo({
        url: '../search?keyword=' + event.detail,
      })
    }
  },
  //分享按钮
  onShareAppMessage: function () {
    var config = app.globalData.appConfig;
     return {
       title: config.store_share_text,
       imageUrl:config.store_share_img,
       path: '/pages/store/index?ucode=' + app.globalData.loginuser.ucode
     }
  },
});